//import axios from 'axios';

// 使用专门的Shop服务HTTP实例
import { shopHttp as http } from './config';

// 原独立配置（保留备用）
// import { http } from './config';

export interface PageInfo<T> {
  list: T[];
  total: number;
  pageNum: number;
  pageSize: number;
}

export interface Shop {
  id: number | null;
  createTime: string | null;
  updateTime: string | null;
  relaUserId: number | null;
  name: string | null;
  description: string | null;
  logoAddress: string | null;
  address: string | null;
  positionX: number | null;
  positionY: number | null;
  phone: string | null;
  startTime: string | null;
  endTime: string | null;
  minOrderAmount: number | null;
  deliveryFee: number | null;
  avRating: number | null;
  avAmount: number | null;
  monthlyOrderCount: number | null;
  image: string | null;
}

// 原独立HTTP实例配置（保留备用）
// const http = axios.create({
//   baseURL: 'http://82.157.31.31:9004',
//   timeout: 10000,
//   headers: { 'Content-Type': 'application/json' }
// });

// 标准接口一: 根据ID查询
export const selectById = (id: number | null) => {
  return http.get<Shop>(`/shop/${id}`);
};

// 标准接口二: 分页查询全部
export const selectAll = (pageNum = 1, pageSize = 10) => {
  return http.get<PageInfo<Shop>>('/shop', {
    params: { pageNum, pageSize }
  });
};

// 标准接口三: 新增
export const insertShop = (pojo: Shop) => {
  return http.post<Shop>('/shop', pojo);
};

// 标准接口四: 删除
export const deleteById = (id: number) => {
  return http.delete<Shop>(`/shop/${id}`);
};

// 标准接口五: 更新
export const update = (id: number | null, pojo: Shop) => {
  return http.put<Shop>(`/shop/${id}`, pojo);
};

// 标准接口六: 条件查询
export const selectEq = (conditions: Record<string, any>) => {
  return http.post<Shop[]>('/shop/select-eq', conditions);
};

// 扩展接口：搜索店铺（基于后端控制器）
export const searchShops = (keyword: string, pageNum = 1, pageSize = 10) => {
  return http.get<PageInfo<Shop>>('/shop/search', {
    params: { keyword, pageNum, pageSize }
  });
};

// 扩展接口：按评分排序
export const getShopsByRating = (pageNum = 1, pageSize = 10) => {
  return http.get<PageInfo<Shop>>('/shop/av-rating-hign', {
    params: { pageNum, pageSize }
  });
};

// 扩展接口：按距离排序
export const getShopsByDistance = (positionX: number, positionY: number, pageNum = 1, pageSize = 10) => {
  return http.get<PageInfo<Shop>>('/shop/address-near', {
    params: { positionX, positionY, pageNum, pageSize }
  });
};

// 扩展接口：按价格区间查询
export const getShopsByPriceRange = (minPrice: number, maxPrice: number, pageNum = 1, pageSize = 10) => {
  return http.get<PageInfo<Shop>>('/shop/range', {
    params: { avAmountMin: minPrice * 100, avAmountMax: maxPrice * 100, pageNum, pageSize }
  });
};